const express = require('express');
const mongoose = require("mongoose");
const bodyParser = require("body-parser");
const passport = require("passport");
const app = express();

//引入user.js
const users = require("./routes/api/users");
const profiles = require("./routes/api/profiles");
//DB config
const db = require("./config/keys").mongoURI;

//使用body-Parser中间件
app.use(bodyParser.urlencoded({extended:false}));
app.use(bodyParser.json());



//Connect to mongodb
mongoose.connect(db,{ useNewUrlParser: true, useUnifiedTopology: true })
        .then(() => console.log("MongoDB Connected"))
        .catch(err => console.log(err));

//passport进行初始化
app.use(passport.initialize());

require("./config/passport")(passport);

// app.get("/",(req,res) => {
//   res.end("Hello World!");
// })


const port = process.env.PORT || 5000;

//使用routes
app.use("/api/users",users);
app.use("/api/profiles",profiles);

app.listen(port,() => {
  console.log(`Server running on port ${port}`);
})